#pragma once
#include "Common.h"
#include <list>
#include <iostream>

class PageCache
{
public:

    Span *NewSpan(size_t numpage);

    void ReleaseSpanToPageCache(Span *span);

    Span *GetIdToSpan(PAGE_ID id);

    static PageCache &GetPageCacheInstance()
    {
        static PageCache inst;
        return inst;
    }
    void Lock()
    {
        mtx.lock();
    }
    void Unlock()
    {
        mtx.unlock();
    }

private:
    PageCache() {}
    PageCache(const PageCache &) = delete;
    ~PageCache()
    {
        for (auto ptr : memory) {
            SystemFree(ptr);
        }
        memory.clear();
    }
    std::list<void *> memory;
    SpanList spanLists[MAX_PAGES];
    // std::unordered_map<PAGE_ID, Span*>  idSpanMap;
    HashMap<PAGE_ID, Span *, HashFunc> idSpanMap;
    ObjectPool<Span> pool;

    std::mutex mtx;
};